//
//  GYStoreIntroduceViewController.m
//  GoodLuck
//
//  Created by apple on 2018/3/14.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYStoreIntroduceViewController.h"
#import "GYUser.h"
#import "GYNetWorkTools.h"
#import "GYStoreIntroduceFirstCell.h"
#import "GYStoreIntroduceSecondCell.h"
#import "GYStore.h"
#import "GYBasePopoverViewController.h"
#import "GYSearchViewController.h"
#import "PopoverAnimation.h"
#import "GYSystemMessageViewController.h"
#import "GYShopingCartViewController.h"


@interface GYStoreIntroduceViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *storeImageView;
@property (weak, nonatomic) IBOutlet UILabel *storeNameLable;
@property (weak, nonatomic) IBOutlet UILabel *storeTypeLable;
@property (weak, nonatomic) IBOutlet UIButton *favoritesBtu;
@property (weak, nonatomic) IBOutlet UILabel *fansLable;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 店铺的数据模型 */
@property (nonatomic, strong) GYStore *store;

/** 标题 */
@property (nonatomic, strong) NSArray *titles;

/** 动画代理 */
@property (nonatomic, strong) PopoverAnimation *animationDelegate;
@end

@implementation GYStoreIntroduceViewController

//收藏按钮点击事件
- (IBAction)favoritesBtuClick:(UIButton *)sender {
    if ([GYUser loadUserInfo] == nil) {
        //跳转到登陆界面
        self.tabBarController.selectedIndex = 2;
        [self.tabBarController.tabBar setHidden:YES];
    }else {
        int num = [self.fansLable.text intValue];
        if (sender.selected) {
            //代表已经收藏    点击就是取消收藏的请求
            [GYNetWorkTools requestCancelStoreFavorites:self.store_id success:^(NSDictionary *obj) {
                //NSLog(@"cacanel=====%@",obj);
                int code = [[obj objectForKey:@"code"] intValue];
                if (code == 200) {
                    [EasyTextView showSuccessText:@"取消收藏成功!"];
                    sender.selected = !sender.selected;
                    self.fansLable.text = [NSString stringWithFormat:@"%d粉丝",num-1];
                    self.storeFavoritesInofCallBack(sender.selected, num-1);
                }else if (code == 400) {
                    [EasyTextView showText:@"取消收藏失败!"];
                    NSLog(@"取消收藏失败...");
                }
            } error:^(NSError *error) {
                [EasyTextView showText:@"取消收藏失败!"];
                NSLog(@"loadCancelStoreFavoritesFailed====%@",error);
            }];
            
        }else {
            [GYNetWorkTools requestStoreFavorites:self.store_id success:^(NSDictionary *obj) {
                //NSLog(@"add=====%@",obj);
                int code = [[obj objectForKey:@"code"] intValue];
                if (code == 200) {
                    [EasyTextView showSuccessText:@"收藏成功!"];
                    sender.selected = !sender.selected;
                    self.fansLable.text = [NSString stringWithFormat:@"%d粉丝",num+1];
                    self.storeFavoritesInofCallBack(sender.selected, num+1);
                }else if (code == 400) {
                    [EasyTextView showText:@"收藏失败!"];
                    NSLog(@"收藏失败...");
                }
            } error:^(NSError *error) {
                [EasyTextView showText:@"收藏失败!"];
                NSLog(@"loadStoreFavoritesFailed====%@",error);
            }];
        }
    }

}


- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化
    [self setupUI];
    
    //加载店铺介绍数据
    [self loadStoreIntroduceInfo];

}
- (void)setupUI {
    self.navigationItem.title = @"店铺介绍";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"more_b" tagret:self action:@selector(rightBtuClick) button:nil];
    self.favoritesBtu.layer.cornerRadius = 4;
    self.storeTypeLable.layer.cornerRadius = 4;
    self.storeTypeLable.layer.masksToBounds = YES;
    
    //设置表格属性
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, CGFLOAT_MIN)];
    
}
- (void)rightBtuClick {
    GYBasePopoverViewController *basePopoverVC = [[GYBasePopoverViewController alloc] initWithNibName:@"GYBasePopoverViewController" bundle:nil];
    __weak typeof(self) weakSelf = self;
    basePopoverVC.popoverViewSelectIndexCallBack = ^(NSInteger index) {
        switch (index) {
            case 0:
            {
                weakSelf.tabBarController.selectedIndex = 0;
                [weakSelf.tabBarController.tabBar setHidden:NO];
            }
                break;
                
            case 1:
            {
                GYSearchViewController *searchVC = [[GYSearchViewController alloc] init];
                [self.navigationController pushViewController:searchVC animated:YES];
            }
                break;
                
            case 2:
            {
                if ([GYUser loadUserInfo] != nil) {
                    GYShopingCartViewController *shopingCartVC = [[GYShopingCartViewController alloc] initWithNibName:@"GYShopingCartViewController" bundle:nil];
                    [weakSelf.navigationController pushViewController:shopingCartVC animated:YES];
                }else {
                    weakSelf.tabBarController.selectedIndex = 2;
                    [weakSelf.tabBarController.tabBar setHidden:YES];
                }
            }
                break;
                
            case 3:
            {
                if ([GYUser loadUserInfo] != nil) {
                    GYSystemMessageViewController *systemMessageVC = [[GYSystemMessageViewController alloc] initWithNibName:@"GYSystemMessageViewController" bundle:nil];
                    [weakSelf.navigationController pushViewController:systemMessageVC animated:YES];
                }else {
                    weakSelf.tabBarController.selectedIndex = 2;
                    [weakSelf.tabBarController.tabBar setHidden:YES];
                }
            }
                break;
                
            default:
                break;
        }
    };
    basePopoverVC.titles = @[@"首页",@"搜索",@"购物车",@"消息"];
    basePopoverVC.images = @[@"home_w",@"search_w",@"ff_icon3",@"message_w"];
    
    self.animationDelegate = [[PopoverAnimation alloc] init];
    self.animationDelegate.popoverFrame = CGRectMake(ScreenWidth-widthScale(130, iphone6_W)-5, 50, widthScale(130, iphone6_W), basePopoverVC.titles.count*50);
    basePopoverVC.transitioningDelegate = (id)weakSelf.animationDelegate;
    
    basePopoverVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:basePopoverVC animated:YES completion:nil];

}

- (void)loadStoreIntroduceInfo {
    [EasyLodingView showLodingText:@"正在加载..."];
    [GYNetWorkTools requestStoreIntroduce:self.store_id success:^(NSDictionary *obj) {
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            [EasyLodingView hidenLoding];
            self.store = [[GYStore alloc] initWithDic:[datas objectForKey:@"store_info"]];
            //赋值
            [self.storeImageView sd_setImageWithURL:[NSURL URLWithString:self.store.store_avatar] placeholderImage:[UIImage imageNamed:@"4"]];
            self.storeNameLable.text = self.store.store_name;
            if (self.store.is_own_shop) {
                self.storeTypeLable.text = @" 旗舰店 ";
            }else {
                self.storeTypeLable.text = @" 普通店铺 ";
            }
            self.fansLable.text = [NSString stringWithFormat:@"%d粉丝",self.store.store_collect];
            self.favoritesBtu.selected = self.store.is_favorate;
            
            //刷新表格
            [self.tableView reloadData];
            
        }else if (code == 400) {
            [EasyTextView showText:@"加载数据错误,请稍后加载!"];
            [EasyLodingView hidenLoding];
            NSLog(@"网络加载出错=====%@",[datas objectForKey:@"error"]);
        }
    } error:^(NSError *error) {
        [EasyTextView showText:@"加载数据错误,请稍后加载!"];
        [EasyLodingView hidenLoding];
        NSLog(@"laodStoreIntroduceFailed====%@",error);
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 2 ? 2 : 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        GYStoreIntroduceFirstCell *cell = [GYStoreIntroduceFirstCell cellWithTableView:tableView];
        switch (indexPath.row) {
            case 0:
            {
                cell.desLable.text = [[self.store.store_credit objectForKey:@"store_desccredit"] objectForKey:@"text"];
                cell.scoreLable.text = [NSString stringWithFormat:@"%0.1f",[[[self.store.store_credit objectForKey:@"store_desccredit"] objectForKey:@"credit"] floatValue]];
            }
                break;
                
            case 1:
            {
                cell.desLable.text = [[self.store.store_credit objectForKey:@"store_servicecredit"] objectForKey:@"text"];
                cell.scoreLable.text = [NSString stringWithFormat:@"%0.1f",[[[self.store.store_credit objectForKey:@"store_servicecredit"] objectForKey:@"credit"] floatValue]];
            }
                break;
                
            case 2:
            {
                cell.desLable.text = [[self.store.store_credit objectForKey:@"store_deliverycredit"] objectForKey:@"text"];
                cell.scoreLable.text = [NSString stringWithFormat:@"%0.1f",[[[self.store.store_credit objectForKey:@"store_deliverycredit"] objectForKey:@"credit"] floatValue]];
            }
                break;
                
            default:
                break;
        }
        
        return cell;
    }else if (indexPath.section == 1){
        GYStoreIntroduceSecondCell *cell = [GYStoreIntroduceSecondCell cellWithTableView:tableView];
        NSArray *array = self.titles[indexPath.section-1];
        cell.headTitleLable.text = array[indexPath.row];
        switch (indexPath.row) {
            case 0:
            {
                cell.contentLable.text = self.store.store_company_name;
            }
                break;
                
            case 1:
            {
                cell.contentLable.text = self.store.area_info;
            }
                break;
                
            case 2:
            {
                cell.contentLable.text = self.store.store_time_text;
            }
                break;
                
            default:
                break;
        }
        
        return cell;
    }else {
        GYStoreIntroduceSecondCell *cell = [GYStoreIntroduceSecondCell cellWithTableView:tableView];
        NSArray *array = self.titles[indexPath.section-1];
        cell.headTitleLable.text = array[indexPath.row];
        if (indexPath.row == 0) {
            cell.contentLable.text = self.store.store_phone;
        }else {
            cell.contentLable.text = self.store.store_workingtime;
        }
        
        return cell;
        
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    view.backgroundColor = RGBColor(245, 245, 245);
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

#pragma mark -- 懒加载 

- (NSArray *)titles {
    if (!_titles) {
        _titles = @[@[@"公司名称",@"所在地",@"开店时间"],@[@"联系电话",@"工作时间"]];
    }
    
    return _titles;
}
@end
